// firstPackage/pages/reportModule/reportRecordDetail/reportRecordDetail.js

import {
  queryRecommendDetail,
  updateRecommendDetail
} from "../../../../service/custom_service"
import {
  chooseMultipleImgUploadQiniuAction
} from "../../../../service/upload_service"
import { checkCompanyRecordAction } from "../../../../service/manager_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    bottomTitle: "提交",
    showCommissionPop: false,
    followArr: [

    ],
    stepActive: 0,//当前审核步骤
    checkArr: [
      { type: 1 }, { type: 1 }, { type: 1 }
    ],
    detailId: null,//详情id
    formData: {

    },//详情数据
    //提交数据
    subFormData: {
      buyHouseNumber: "",
      id: null,
      images: [],
      recordId: null,
      status: null,
      totalPrice: null,
    },
    //报备列表
    recordList: [

    ],
    checkData: {
      "id": 0,
      "isPass": false,
      "isReportDeveloper": false,
      "remark": "",//拒绝的理由
      "url": "",
      "money": "",//已签约过后输入佣金
    },
    currentSubmitStatus: null,//最终提交数据的status
    lastExamineStatus: null,
    isShowCommentPop: false,//显示不通过理由
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        detailId: options.id
      })
    }

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryRecommendDetail();
  },

  //查询报备详情:
  queryRecommendDetail() {
    wx.showLoading();
    queryRecommendDetail(this.data.detailId).then(res => {
      wx.hideLoading();
      console.log("JJLog:获取到的报备详情是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          formData: res.data,
          recordList: res.data.records,
          lastExamineStatus: res.data.records[res.data.records.length - 1].examineStatus,
          currentSubmitStatus: res.data.status
        })
        this.setData({
          'checkData.id': res.data.id,
        })

      }
    }).catch(err => {
      console.error("JJLog:queryRecommendDetail", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },

  //上传资料图片
  pushRecordImg(e) {
    let index = e.currentTarget.dataset.index;
    let recordItem = e.currentTarget.dataset.item;
    let imgArr = recordItem.images || [];
    let status = recordItem.status;
    chooseMultipleImgUploadQiniuAction('recordImg').then(res => {
      console.log("JJLog:图片上传成功:" + JSON.stringify(res));
      wx.showToast({
        title: '上传成功',
      })
      res.forEach((item) => {
        let obj = {
          "type": 1,
          "url": item.complateUrl,
        }
        imgArr.push(obj);
      })
      recordItem.images = imgArr;
      let changeItem = 'recordList[' + index + ']';
      this.setData({
        [changeItem]: recordItem,
        'subFormData.images': imgArr,
        'subFormData.status': status,
        'subFormData.recordId': recordItem.id
      })

    }).catch(err => {
      console.log("JJLog:图片上传失败:" + JSON.stringify(err));
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })

  },
  //删除一张图片
  cancelOneImg(e) {
    let item = e.currentTarget.dataset.item;
    let imgIndex = e.currentTarget.dataset.imgindex;
    let index = e.currentTarget.dataset.index;
    let images = item.images;
    images.splice(imgIndex, 1);
    images.images = images;
    let changeItem = 'recordList[' + index + ']';
    this.setData({
      [changeItem]: recordItem,
      'subFormData.images': images,
    })
  },

  //提交数据:
  submitAction() {
    if (this.data.lastExamineStatus == 1) {
      wx.showToast({
        title: "资料审核中，无法提交",
        icon: "none"
      })
      return;
    }
    if (this.data.lastExamineStatus == 2) {
      wx.showToast({
        title: "资料审核成功，不能修改",
        icon: "none"
      })
      return;
    }
    debounce(this.submitInfoRequest);
  },
  //提交数据请求;
  submitInfoRequest() {
    console.log(this.data.subFormData);
    //临时这样写
    if (this.data.subFormData.status == 3) {
      this.setData({
        'subFormData.buyHouseNumber': "1004",
        'subFormData.totalPrice': "123"
      })
    }
    wx.showLoading();
    updateRecommendDetail(this.data.subFormData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '提交成功',
          icon: "none"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000);
      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:updateRecommendDetail", err);
    })
  },



  submitInfoAction(e) {
    let isPass = e.currentTarget.dataset.ispass;
    this.setData({
      'checkData.isPass': isPass
    })
    // if (this.data.currentSubmitStatus == 4) {
    //   this.setData({
    //     showCommissionPop: true
    //   })
    //   return;
    // }
    if (isPass) {
      //通过审核:
      wx.showModal({
        title: '提示',
        content: '确认资料通过审核',
        confirmColor: "#FF0000",
        success: res => {
          if (res.confirm) {
            this.submitInfoRequest();
          }
        }
      })

    } else {
      //拒绝报备
      this.setData({
        isShowCommentPop: true
      })
    }
  },
  //提交审核结果
  submitInfoRequest() {
    wx.showLoading();
    console.log("JJLog:审核提交参数:" + JSON.stringify(this.data.checkData));
    checkCompanyRecordAction(this.data.checkData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: "提交成功",
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000)
      }
    }).catch(err => {
      console.error("JJLog:checkCompanyRecordAction", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "审核失败",
        icon: "none"
      })
    })
  },

  //发送审核失败信息
  commentSend(e) {
    console.log(e);
    let content = e.detail.comment;
    this.setData({
      'checkData.remark': content,
      isShowCommentPop: false
    })
    this.submitInfoRequest();


  },
  commissionSend(e) {
    let content = e.detail.comment;
    if (!this.checkNumber(content)) {
      wx.showToast({
        title: '佣金只能输入数字',
        icon: "none"
      })
      return
    }
    this.setData({
      'checkData.money': content,
      showCommissionPop: false
    })
    this.submitInfoRequest();
  },
  //验证字符串是否是数字
  checkNumber(theObj) {
    let reg = /^[0-9]+.?[0-9]*$/;
    if (reg.test(theObj)) {
      return true;
    }
    return false;
  },
  //隐藏输入框:
  hideCommentPop() {
    this.setData({
      isShowCommentPop: false,
    })
  },

  //预览图片:
  previewImgs(e) {
    let images = e.currentTarget.dataset.images;
    let url = e.currentTarget.dataset.url;
    let urls = [];
    images.forEach((i) => {
      urls.push(i.url)
    })
    wx.previewImage({
      current: url,
      urls: urls,
    })
  },
  previewCodeImg(e) {
    let url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url,
      urls: [url],
    })
  },
  hideCommissionPop() {
    this.setData({
      showCommissionPop: false,
    })
  },



})